{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课程1.5---Numpy数据的输入与输出\n",
    "## Numpy读取/写入数组数据，文本数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.13389287 0.964937   0.21994744 0.02316549 0.91121663]\n",
      " [0.60693404 0.46538374 0.68338372 0.3042743  0.94794604]\n",
      " [0.57445204 0.96189776 0.04712687 0.14613759 0.01298376]\n",
      " [0.97465597 0.25768499 0.95910334 0.66669867 0.22205536]\n",
      " [0.84205466 0.64499971 0.78520505 0.87420594 0.7727194 ]]\n"
     ]
    }
   ],
   "source": [
    "# 存储数组数据 .npy文件\n",
    "\n",
    "import numpy as np\n",
    "import os\n",
    "os.chdir(r\"D:\\Python练习\")\n",
    "\n",
    "ar = np.random.rand(5,5)\n",
    "print(ar)\n",
    "np.save(\"arraydata.npy\",ar)\n",
    "# 也可以直接 np.save(r\"D:\\Python练习\\arraydata.npy\", ar)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.13389287 0.964937   0.21994744 0.02316549 0.91121663]\n",
      " [0.60693404 0.46538374 0.68338372 0.3042743  0.94794604]\n",
      " [0.57445204 0.96189776 0.04712687 0.14613759 0.01298376]\n",
      " [0.97465597 0.25768499 0.95910334 0.66669867 0.22205536]\n",
      " [0.84205466 0.64499971 0.78520505 0.87420594 0.7727194 ]]\n"
     ]
    }
   ],
   "source": [
    "# 读取数组文件 .npy文件\n",
    "\n",
    "ar_load = np.load(\"arraydata.npy\")\n",
    "print(ar_load)\n",
    "# 也可以直接np.load(r\"# 存储数组数据 .npy文件）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.84976815 0.05472687 0.46660554 0.50229243 0.07287162]\n",
      " [0.54284133 0.03102612 0.42999545 0.55241157 0.81341422]\n",
      " [0.00574233 0.0480327  0.29999644 0.07674653 0.28178927]\n",
      " [0.60563717 0.87023471 0.44676801 0.50214281 0.6035047 ]\n",
      " [0.26137627 0.69369219 0.12449053 0.11795134 0.71186916]]\n"
     ]
    }
   ],
   "source": [
    "# 存储/读取文本文件\n",
    "\n",
    "ar = np.random.rand(5,5)\n",
    "np.savetxt(\"array.txt\",ar,delimiter = ',')\n",
    "# np.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\\n', header='', footer='', comments='# ')：存储为文本txt文件\n",
    "\n",
    "ar_loadtxt = np.loadtxt('array.txt', delimiter=',')\n",
    "print(ar_loadtxt)\n",
    "# 也可以直接 np.loadtxt(r\"D:\\Python练习\\arraydata.txt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
